package com.ax.base.common.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ax.base.common.domain.BaseOrg;
import com.ax.base.common.domain.Tree;
import com.ax.base.system.domain.MenuDO;


public interface BaseOrgMapper {
    int deleteByPrimaryKey(Long orgId);

    int insert(BaseOrg record);

    int insertSelective(BaseOrg record);
    /**
	 * 组织机构查询(模块:组织机构)
	 * 
	 * @param Map<String,Object> map
	 *           
	 * @return BaseOrg baseOrg
	 */
    List<BaseOrg> selectByPrimaryKey(Map<String,Object> map);
    
    /**
	 * 组织机构(模块:组织机构)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseOrg baseOrg
	 */
    @Select("<script>"+
    "select "+ 
    "Org_ID menuId, Org_Name name, Org_Short_Name orgShortName, First_Letter firstLetter, Area_Code areaCode,"+ 
    "Area_Name areaName, Grade_Code gradeCode, Grade_Name gradeName, Introduction introduction, Phone phone,"+ 
    "Address address, Longitude longitude, Latitude latitude, Remarks remarks, Sort_Number sortNumber,"+ 
    "Parent_Org_ID parentId, Org_Type_Code orgTypeCode, Org_Type_Name orgTypeName, create_date createDate, update_date updateDate, Flag flag "+
    "from base_org"+
    "<where>"+
    "<if test=\"orgId != null\">"+ "and Org_ID = #{orgId} " + "</if>" + 
    "</where>"+
    "</script>")
    BaseOrg select(@Param("orgId") Long orgId);
    
    /**
	 * 组织机构查询(模块:组织机构)
	 * 
	 * @param Map<String,Object> map
	 *           
	 * @return BaseOrg baseOrg
	 */
    @Select("<script>"+ 
    		"select Org_ID menuId, Org_Name name, Org_Short_Name orgShortName,"+
    		 "First_Letter firstLetter,"+ 
    		 "Area_Code areaCode, Area_Name areaName, Grade_Code gradeCode,"+ 
    		 "Grade_Name gradeName, Introduction introduction, Phone phone, Address address, Longitude longitude,"+
    		 "Latitude latitude, Remarks remarks, Sort_Number sortNumber, "+
    		 "Parent_Org_ID parentId, Org_Type_Code orgTypeCode, Org_Type_Name orgTypeName,"+
    		 "create_date createDate, update_date updateDate, Flag flag "+
    		 "from base_org"+
    		 "<where>"+
    		 "<if test=\"orgId != null\">"+ "and Org_ID = #{orgId} " + "</if>" + 
    		 "</where>"+
    		 "</script>")
    List<BaseOrg> queryBaseOrgMapper(Map<String, Object> params);
    
    /**
	 * 组织机构新增(模块:组织机构)
	 * 
	 * @param BaseOrg baseOrg
	 *           
	 * @return SUCC
	 */
    @Insert("insert into base_org (Org_ID, Org_Name, Org_Short_Name,"+ 
		    "First_Letter, Area_Code, Area_Name,"+ 
		      "Grade_Code, Grade_Name, Introduction,"+ 
		      "Phone, Address, Longitude,"+ 
		      "Latitude, Remarks, Sort_Number,"+ 
		      "Parent_Org_ID, Org_Type_Code, Org_Type_Name,"+ 
		      "create_date, update_date, Flag"+
		      ")"+
		    "values (#{orgId}, #{orgName}, #{orgShortName},"+ 
		     "#{firstLetter}, #{areaCode}, #{areaName},"+ 
		      "#{gradeCode}, #{gradeName}, #{introduction},"+ 
		      "#{phone}, #{address}, #{longitude},"+ 
		      "#{latitude}, #{remarks}, #{sortNumber},"+ 
		      "#{parentOrgId}, #{orgTypeCode}, #{orgTypeName},"+ 
		      "NOW(), #{updateDate}, #{flag}"+
		      ")"
		      )
    void insertBaseOrgMapper(BaseOrg baseOrg);
    
    @Update("<script>"+ 
    		"update base_org"+
			"<set>"+
			"<if test=\"orgName != null\">"+ "Org_Name = #{orgName}," + "</if>" +
			"<if test=\"orgShortName != null\">"+ "Org_Short_Name = #{orgShortName}," + "</if>" +
			"<if test=\"firstLetter != null\">"+ "First_Letter = #{firstLetter}," + "</if>" +
			"<if test=\"areaCode != null\">"+ "Area_Code = #{areaCode}," + "</if>" +
			"<if test=\"areaName != null\">"+ "Area_Name = #{areaName}," + "</if>" +
			"<if test=\"gradeCode != null\">"+ "Grade_Code = #{gradeCode}," + "</if>" +
			"<if test=\"gradeName != null\">"+ "Grade_Name = #{gradeName}," + "</if>" +
			"<if test=\"introduction != null\">"+ "Introduction = #{introduction}," + "</if>" +
			"<if test=\"phone != null\">"+ "Phone = #{phone}," + "</if>" +
			"<if test=\"address != null\">"+ "Address = #{address}," + "</if>" +
			"<if test=\"longitude != null\">"+ "Longitude = #{longitude}," + "</if>" +
			"<if test=\"latitude != null\">"+ "Latitude = #{latitude}," + "</if>" +
			"<if test=\"remarks != null\">"+ "Remarks = #{remarks}," + "</if>" +
			"<if test=\"sortNumber != null\">"+ "Sort_Number = #{sortNumber}," + "</if>" +
			"<if test=\"parentOrgId != null\">"+ "Parent_Org_ID = #{parentOrgId}," + "</if>" +
			"<if test=\"orgTypeCode != null\">"+ "Org_Type_Code = #{orgTypeCode}," + "</if>" +
			"<if test=\"orgTypeName != null\">"+ "Org_Type_Name = #{orgTypeName}," + "</if>" +
			"<if test=\"createDate != null\">"+ "create_date = #{createDate}," + "</if>" +
			"update_date = NOW(),"+
			"<if test=\"flag != null\">"+ "Flag = #{flag}," + "</if>" +
			"</set>"+
			"<where>"+
			"<if test=\"orgId != null\">"+ "and Org_ID = #{orgId}" + "</if>" +
			"</where>"+
			"</script>" )
    void updateBaseOrgMapper(BaseOrg baseOrg);
    
    /**
	 * Tree
	 * 
	 * @param Long parentOrgId
	 *           
	 * @return Tree<MenuDO>
	 */
    @Select("SELECT Org_ID id,Org_Name text, Parent_Org_ID parentId FROM base_org")
    List<Tree<MenuDO>> listOrg();
    
    int updateByPrimaryKeySelective(BaseOrg record);

    int updateByPrimaryKey(BaseOrg record);
}